package ar.com.mej.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.Properties;

public class Utils {

	public static boolean controlString(String value)
	{
		if(value != null && value.trim().length() > 0)
		{
			return true;
		}		
		return false;
	}	
	
	private static boolean isNumeric(String cadena){
		try {
			Long.parseLong(cadena);
			return true;
		} catch (NumberFormatException nfe){
			return false;
		}
	}
	
	//Agrega puntos cada mil
	public static String generateValue(String value)
	{
		if(isNumeric(value))
		{
			StringBuilder newValue = new StringBuilder();
			int length = value.length();
			int cont = 0;
			while(length > 0)
			{
				if(cont == 3)
				{
					newValue.append(".");
					newValue.append(value.charAt(length-1));
					cont = 0;
				}
				else
				{
					newValue.append(value.charAt(length-1));	
				}			
				cont++;
				length--;
			}
			return newValue.reverse().toString();
		}
		return value;
	}
	
	public static Properties getProperties(String filePath)
	{
		try {
			Properties props = new Properties();
			InputStream resource = Utils.class.getResourceAsStream("../" + filePath);
			props.load(resource);
			resource.close();
			return props;	
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}
	
	public Properties getPropertiesByResource(String resourceName)
	{
		InputStream resource = null;
		try {
			Properties props = new Properties();
			resource = this.getClass().getClassLoader().getResourceAsStream(resourceName);
			props.load(resource);
			
			return props;	
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		finally
		{
			if(resource != null)
			{
				try {
					resource.close();
				} catch (IOException e) {
					e.printStackTrace();
				}	
			}			
		}
	}	
	
	
	public static String getStackTrace(Exception exception)
	{
		StringWriter traceWriter = new StringWriter();
		PrintWriter printWriter = new PrintWriter(traceWriter, false);
		exception.printStackTrace(printWriter);
		printWriter.close();
		String faultMessage = traceWriter.getBuffer().toString();
		
		return faultMessage;
	}
	
	public static boolean valueIsMayorThanList(String value, List<String> values)
	{
		boolean isMayor = true;
		Long valueLong = new Long(value);
		for(String val : values)
		{
			Long valLong = new Long(val);
			if(valLong > valueLong)
			{
				isMayor = false;
				break;
			}
		}		
		return isMayor;
	}	
}
